package org.dream.ui.chart.impl;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;

import org.dream.ui.chart.YAxis;
import org.dream.ui.chart.YAxisModel;
import org.dream.util.DoubleUtil;

/**
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class YAxisImpl extends AxisImpl implements YAxis {

	private YAxisModel yAxisModel;

	public YAxisImpl(int tickCount, Double heightScan) {
		yAxisModel = new YAxisModelImpl(tickCount, heightScan);
	}

	public YAxisModel getYAxisModel() {
		return yAxisModel;
	}

	@Override
	public int getLabelWidth(Graphics g) {
		int tickLabelWidth = g.getFontMetrics().stringWidth("100.35");
		return tickLabelWidth;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.dream.ui.chart.YAxis#draw(java.awt.Graphics)
	 */
	public void draw(Graphics g) {
		if (null == yAxisModel.getIndex()) {
			throw new RuntimeException("y轴必须有序号");
		}
		yAxisModel.beforeDraw();
		/* 绘制纵坐标轴 */
		g.setColor(axisColor);
		g.drawLine(yAxisModel.getPixelXRight(), yAxisModel.getPixelYTop(), yAxisModel.getPixelXRight(),
				yAxisModel.getPixelYButtom());

		Graphics2D g2 = (Graphics2D) g;
		Stroke strokeOld = g2.getStroke();
		/** 画网格线 */
		drawGrid(g, g2);
		g2.setStroke(strokeOld);
		/* 绘制刻度线 */
		drawTickLine(g);
		// 画刻度线的值
		drawTickLineValue(g);
		g2.setStroke(strokeOld);

		/** 如果是多个窗口，在上面几个窗口下画一横线 */
		if (yAxisModel.isdrawButtomLine()) {
			g.drawLine(yAxisModel.getPixelXLeft(), yAxisModel.getPixelYButtom(), yAxisModel.getPixelXRight(),
					yAxisModel.getPixelYButtom());
		}
		g2.setPaint(Color.BLACK);
	}

	private void drawGrid(Graphics g, Graphics2D g2) {
		float dash[] = { 1.0f };
		// 从下住上画
		for (int i = 1; i <= yAxisModel.getTickCount(); i++) {
			g2.setStroke(new BasicStroke(0.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dash, 0.0f));
			// y坐标网格线
			int tickPixelY = yAxisModel.getTickPixelY(i);
			if (yAxisModel.isDrawGrid()) {
				/* 如果drawGrid属性为true,用gridColor绘制网格线 */
				g.setColor(gridColor);
				g.drawLine(yAxisModel.getPixelXLeft(), tickPixelY, yAxisModel.getPixelXRight(), tickPixelY);
			}

		}
	}

	private void drawTickLine(Graphics g) {
		for (int i = 1; i <= yAxisModel.getTickCount(); i++) {
			int tickPixelY = yAxisModel.getTickPixelY(i);
			/* 绘制刻度线 */
			g.setColor(axisColor);
			g.drawLine(yAxisModel.getPixelXRight(), tickPixelY,
					yAxisModel.getPixelXRight() + yAxisModel.getTickLineLength(), tickPixelY);
			// 在下面画5个小刻度线
			for (int j = 1; j <= 3; j++) {
				g.drawLine(yAxisModel.getPixelXRight(), (int) (tickPixelY + j * yAxisModel.getTickDistPixel() / 4),
						yAxisModel.getPixelXRight() + yAxisModel.getTickLineLength() / 3 * (j % 2 == 0 ? 3 : 1),
						(int) (tickPixelY + j * yAxisModel.getTickDistPixel() / 4));
			}
		}
	}

	private void drawTickLineValue(Graphics g) {
		for (int i = 1; i <= yAxisModel.getTickCount(); i++) {
			// 画刻度线显示值
			int tickPixelY = yAxisModel.getTickPixelY(i);
			double y = (i) * yAxisModel.getTickDist() + yAxisModel.getMinValue();
			int tickLabelWidth = g.getFontMetrics().stringWidth(String.valueOf(DoubleUtil.changeDecimal(y, 2)));
			int xPoint = yAxisModel.getPixelXRight() + yAxisModel.getTickLineLength();
			int yPoint = tickPixelY + g.getFontMetrics().getHeight() / 4;
			g.drawString(String.valueOf(DoubleUtil.changeDecimal(y, 2)), xPoint, yPoint);
		}
	}

}
